﻿using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Text;

namespace WSOAMOBIMIDWARE
{
    public class XmlService
    {
        private static string RemoveText(Match m) { return ""; }
        /// <summary>
        /// 反序列化为xml对象
        /// </summary>
        public static void DeSerializeFromXML<T>(out T obj, string xmlData)
        {
            XmlSerializer xmlSerializer = null;
            //XmlTextWriter xmlTextWriter = null;
            //MemoryStream memoryStream = null;
            TextReader textReader = null;
            try
            {
                string cleanXml = Regex.Replace(xmlData, @"<[a-zA-Z].[^(><.)]+/>",
                                        new MatchEvaluator(RemoveText));
                cleanXml = Regex.Replace(cleanXml, @"<[a-zA-Z]+></[a-zA-Z]+>",
                                        new MatchEvaluator(RemoveText));
                //cleanXml = cleanXml.Replace("<url></url>", "");
                //cleanXml = cleanXml.Replace("<img></img>", "");
                //cleanXml = cleanXml.Replace("<pvalue></pvalue>", "");
                textReader = new StringReader(cleanXml);
                xmlSerializer = new XmlSerializer(typeof(T));
                obj = (T)xmlSerializer.Deserialize(textReader);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (textReader != null)
                    textReader.Close();
                xmlSerializer = null;
            }

        }

        /// <summary>
        /// 序列化为xml
        /// </summary>
        public static string SerializeToXML<T>(T obj)
        {
            XmlSerializer xmlSerializer = null;
            StringWriter stringWriter = null;
            string xmlString = string.Empty;
            try
            {
                stringWriter = new StringWriterWithEncoding(Encoding.UTF8);
                xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(stringWriter, obj);
                xmlString = stringWriter.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (stringWriter != null) 
                    stringWriter.Close();
                xmlSerializer = null;
            }
            return xmlString;
        }
    }

    public class StringWriterWithEncoding : StringWriter
    {
        private Encoding myEncoding;
        public override Encoding Encoding
        {
            get
            {
                return myEncoding;
            }
        }
        public StringWriterWithEncoding(Encoding encoding)
            : base()
        {
            myEncoding = encoding;
        }
    }
}
